package com.lanou3g.ptp.api.web.controller;

import com.lanou3g.ptp.api.entity.User;
import com.lanou3g.ptp.api.utils.PtpResult;
import com.lanou3g.ptp.api.utils.PtpResultUtils;
import com.lanou3g.ptp.api.utils.EntityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Api(description = "用户接口")
@RestController
public class UserController {

    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "login", value = "登录账号（手机号、姓名、编号）"),
            @ApiImplicitParam(name = "password", value = "密码")
    })
    @PostMapping("/user/login")
    public PtpResult login(String login,
                           String password) {
        System.out.println(login);
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        return PtpResultUtils.ok(result);
    }

    @ApiOperation(value = "禁用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID")
    })
    @PutMapping("/user/disable/{id}")
    public PtpResult disableUser(@PathVariable Long id) {
        System.out.println(id);
        return PtpResultUtils.ok();
    }

    @ApiOperation(value = "启用用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID")
    })
    @PutMapping("/user/enable/{id}")
    public PtpResult enableUser(@PathVariable Long id) {
        System.out.println(id);
        return PtpResultUtils.ok();
    }

    @ApiOperation(value = "通过ID获取用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要获取的用户的ID", example = "1")
    })
    @GetMapping("/user/{id}")
    public PtpResult findById(@PathVariable Long id) {
        User user = EntityUtils.random(User.class);
        user.setId(id);
        return PtpResultUtils.ok(user);
    }

    @ApiOperation(value = "获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页数据量（建议不传）", defaultValue = "30"),
            @ApiImplicitParam(name = "keyword", value = "查询关键词"),
    })
    @GetMapping("/users")
    public PtpResult list(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "30") int size,
                          @RequestParam(defaultValue = "") String keyword) {
        System.out.println(page + "\t" + size + "\t" + keyword);
        List<User> users = EntityUtils.random(User.class, size);
        return PtpResultUtils.ok(users);
    }

}
